// subPages/pensionService/reserveComfirm/reserveComfirm.js
const app = getApp();
var util =  require('../../../utils/util.js');
var isBuy = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:app.globalData.userId,
    isOpen:false,
    ProductLen:'',
    yunfei:0
  },
    //获取地址数据
    getAddressData:function(){
        var that = this
        util.getAjax(app.globalData.apiUrl +'/get_user_address',function(res){
            console.log(res)
            if(res.data.length!=0){
                that.setData({
                    isAddress: true,
                    addressList: res.data[0],
                })
            }else{
                that.setData({
                    isAddress: false,
                    addressList: [],
                })
                util.showModelAll('提示','请添加收货地址',function(){
                    wx.navigateTo({
                        url: '/subPages/mine/newAddress/newAddress?isOne=' + 1,
                    })
                })
            }
        },{type:1,user_id: that.data.userId})
    },
    //地址管理
    addressBtn:function(){
        wx.navigateTo({
            url: '/subPages/mine/address/address',
        })
    },
    //新增地址管理
    newAddressBtn:function(){
        wx.navigateTo({
            url: '/subPages/mine/newAddress/newAddress?isOne=' + 1,
        })
    },
    //获取厨房主体
    getKitchen(){
        let _this = this;
        util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen`,res=>{
            _this.setData({
                kitchenId:res.data.id
            })
        },{
            community_id:app.globalData.communityId
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      //type 1,单日  全部
      var type = options.type;
      var productId = options.productid.split(',');
      console.log(productId);
      this.setData({
          productId:productId
      })
      this.getKitchenList(productId)
      this.getAddressData();
      this.getKitchen()
  },
    getKitchenList(productId){
        var _this = this;
        var productId = productId;
        console.log(productId);
        var productList = [];
        var totalPay = 0;
        if(productId.length>3&&!_this.data.isOpen){
            console.log('jin');
            _this.setData({
                isOpen:true,
                productLen:3
            })
            // productId = productId.slice(0,3);
        }else{
            _this.setData({
                isOpen:false,
                productLen:productId.length
            })
        }
        // console.log(productId);
        for(var i in productId){
            util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen_product_content`,res=>{
                console.log(res);
                res.data.week = util.getWeek(res.data.date);
                productList.push(res.data)
                totalPay += Number(res.data.price);
                _this.setData({
                    productList:productList,
                    totalPay:Number(totalPay.toFixed(2))
                })
            },{
                id:productId[i]
            })
        }
    },
    checkAll(){
      var _this = this;
      var productId = _this.data.productId;
        console.log(productId);
        _this.getKitchenList(productId);
      _this.setData({
          isOpen:false,
          productLen:productId.length
      })
    },
    buy(){
      var _this = this;
      var order_content = {
          user_id: app.globalData.userId,
          order_price:Number(_this.data.totalPay)+Number(_this.data.yunfei),
          kitchen_id:_this.data.kitchenId,//获取厨房id
          sheng:_this.data.addressList.sheng,
          shi:_this.data.addressList.shi,
          qu:_this.data.addressList.qu,
          address:_this.data.addressList.address,
          name:_this.data.addressList.name,
          phone:_this.data.addressList.phone
      }
      var all_product = [];
      var productList = _this.data.productList;
      for(var i in productList){
          var pro = {
              product_id:productList[i].id,
              num:1,
              one_price:productList[i].price,
              all_price:productList[i].price*1
          }
          all_product.push(pro)
      }
      order_content = JSON.stringify(order_content);
      all_product = JSON.stringify(all_product);
      var all_id = [];
        console.log(order_content,all_product);
        util.getAjax(`${app.globalData.apiUrl}/community_service_kitchen_pay`,res=>{
          console.log(res);
          var orderNum = res.data.out_trade_no;
          wx.requestPayment({
              timeStamp:res.data.timeStamp,
              nonceStr:res.data.nonceStr,
              package:res.data.package,
              signType:res.data.signType,
              paySign:res.data.paySign,
              success(res){
                  if(res.errMsg == 'requestPayment:ok'){
                      util.getAjax(`${app.globalData.apiUrl}/community_service_kitchen_pay_ok`,res=>{
                          console.log(res);
                          if(res.data == 1){
                              // console.log('支付成功')
                              wx.navigateTo({
                                  url:'/subPages/mine/orderDetails/orderDetails?orderNum='+orderNum+'&orderclass='+2
                              })
                              isBuy = false
                          }
                      },{
                          order_num:orderNum
                      })
                  }
              },
              fail(err){
                  console.log(err);
                  console.log(11);
                  isBuy = false;
                  wx.navigateTo({
                      url:'/subPages/mine/orderDetails/orderDetails?orderNum='+orderNum+'&orderclass='+2
                  })
              }

          })
      },{
            order_content:order_content,
            all_product:all_product,
            all_id:all_id,
            user_id:app.globalData.userId
      })

    },
    getKitchenDetails(id){
        let _this = this;
        util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen_product_content`,res=>{
            console.log(res);
            _this.setData({
                kitchenInfo:res.data
            })
        },{
            id:id
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this
      if(app.globalData.userId){
          that.setData({
              userId: app.globalData.userId
          })
          that.getAddressData()
      }else{
          app.userIdCallback = userId => {
              if (userId != '') {
                  that.setData({
                      userId: userId
                  });
                  that.getAddressData()
              }
          }
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
